﻿using Gonte.Algorithms;
namespace Gonte.Wpf.Controls.ViewModel
{
    public class ContentViewModelInfo
        : ItemViewModelInfo
    {
        #region Methods

        public static new ItemViewModelInfo GetInfo(string viewModelInfoName)
        {
            if (!_viewModelInfos.ContainsKey(viewModelInfoName))
            {
                ContentViewModelInfo viewModelInfo = new ContentViewModelInfo();
                viewModelInfo.Populate();
                AddViewModelInfo(viewModelInfoName, viewModelInfo);

                return viewModelInfo;
            }

            return _viewModelInfos[viewModelInfoName];
        } 

        #endregion

        /// <summary>
        /// Updates the special case icons to the default one
        /// </summary>
        internal override void Populate()
        {
            // Set all the icons to the default one (IconUri) if they are null and the default one was provided
            if (!string.IsNullOrEmpty(IconUri))
            {
                if (string.IsNullOrEmpty(EmptyIconUri))
                {
                    EmptyIconUri = IconUri;
                }

                if (string.IsNullOrEmpty(CollapsedIconUri))
                {
                    CollapsedIconUri = IconUri;
                }

                if (string.IsNullOrEmpty(ExpandedIconUri))
                {
                    ExpandedIconUri = IconUri;
                }
            }

            // If the empty one is still null, then set it as the collapsed one
            if (string.IsNullOrEmpty(EmptyIconUri))
            {
                EmptyIconUri = CollapsedIconUri;
            }
        }

        #region Fields

        /// <summary>
        /// The static label of the content view model
        /// </summary>
        public string Label;

        /// <summary>
        /// The uri of the icon when there are no children objects
        /// </summary>
        public string EmptyIconUri;

        /// <summary>
        /// The uri of the collapsed icon image
        /// </summary>
        public string CollapsedIconUri;

        /// <summary>
        /// The uri of the expanded icon image
        /// </summary>
        public string ExpandedIconUri;

        /// <summary>
        /// Creates a folder for each children property when there are more than one
        /// </summary>
        public bool CreateFolders;

        /// <summary>
        /// Whether to load the children at the moment the user expands the tree
        /// </summary>
        public bool LoadOnDemand;

        #region Children filtering

        /// <summary>
        /// Delegate to filter the child 
        /// </summary>
        /// <param name="model">The model</param>
        /// <returns>True if the child is allowed to be added, false otherwise</returns>
        public delegate bool AllowChildHandler(object model);

        /// <summary>
        /// Filtering delegate
        /// </summary>
        public AllowChildHandler AllowChild;

        #endregion

        #region Children sorting

        /// <summary>
        /// Whether to sort the children
        /// </summary>
        public bool SortChildren;

        #endregion

        #endregion
    }
}
